package com.lcxw.hxzbapi.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lcxw.hxzbapi.dao.mapper.ShortVideoCommentMapper;
import com.lcxw.hxzbapi.entity.pojo.ShortVideoComment;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Component;

@Component
public class ShortVideoCommentService {

    @Resource
    ShortVideoCommentMapper shortVideoCommentMapper;

    public int insert(ShortVideoComment shortVideoComment) {
        return shortVideoCommentMapper.insert(shortVideoComment);
    }

    public int delete(ShortVideoComment shortVideoComment) {
        UpdateWrapper<ShortVideoComment> updateWrapper = new UpdateWrapper<>();
        updateWrapper.set("status", 0);
        updateWrapper.set("comment", "该评论已被删除");
        return shortVideoCommentMapper.update(shortVideoComment, updateWrapper);
    }

    public List<ShortVideoComment> selectCommentList(Integer svId, int pageNum, int pageSize) {
        IPage<ShortVideoComment> iPage = new Page<>(pageNum, pageSize);

        QueryWrapper<ShortVideoComment> wrapper = new QueryWrapper<>();
        wrapper.eq("svId", svId);
        wrapper.eq("parentId", 0);
        wrapper.orderByDesc("commentId");
//        wrapper.eq("status", 1);
        IPage<ShortVideoComment> shortVideoCommentIPage = shortVideoCommentMapper
            .selectPage(iPage, wrapper);
        return shortVideoCommentIPage.getRecords();
    }

    public List<ShortVideoComment> selectSubCommentList(Integer parentId) {
        QueryWrapper<ShortVideoComment> wrapper = new QueryWrapper<>();
        wrapper.eq("parentId", parentId);
//        wrapper.eq("status", 1);
        wrapper.orderByDesc("commentId");
        return shortVideoCommentMapper.selectList(wrapper);
    }

    //获取某一短视频的评论数量
    public int getCommentNum(int svId) {
        QueryWrapper<ShortVideoComment> wrapper = new QueryWrapper<>();
        wrapper.eq("svId", svId);
        return shortVideoCommentMapper.selectCount(wrapper);
    }

    //通过commentId获取一条评论
    public ShortVideoComment getOne(int CommentId) {
        QueryWrapper<ShortVideoComment> wrapper = new QueryWrapper<>();
        wrapper.eq("CommentId", CommentId);
        return shortVideoCommentMapper.selectOne(wrapper);
    }

    //短视频评论点赞
    public int giveLike(ShortVideoComment shortVideoComment) {
        UpdateWrapper<ShortVideoComment> updateWrapper = new UpdateWrapper<>();
        updateWrapper.setSql("`like` = `like` + 1");
        updateWrapper.eq("commentId", shortVideoComment.getCommentId());
        return shortVideoCommentMapper.update(null, updateWrapper);
    }

    //短视频评论取消
    public int unGiveLike(ShortVideoComment shortVideoComment) {
        UpdateWrapper<ShortVideoComment> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("commentId", shortVideoComment.getCommentId());
        updateWrapper.setSql("`like` = `like` - 1");
        return shortVideoCommentMapper.update(null, updateWrapper);
    }
}
